/** 输入是否全为空格 */
export const isAir = (value: string | number): boolean => {
  let str: string = value + "";
  return str.split(" ").join("").length ? false : true;
};

/** 输入内容是否全为特殊字符 */
export const isSpec = (value: string | number): boolean => {
  let str: string = value + "";
  var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
  return pattern.test(str);
};

/** 验证中文名字是否 2-5个中文字符 */
export const isName = (value: string | number): boolean => {
  let str: string = value + "";
  return RegExp(/^[\u4e00-\u9fa5]{2,5}$/).test(str);
};

/** 验证是否为身份证 */
export const isIDC = (value: string | number): boolean => {
  let str: string = value + "";
  return RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/).test(str);
};

/** 验证是否为手机号 */
export const isPhone = (value: string | number) => {
  let str: string = value + "";
  return RegExp(/^1[345789]\d{9}$/).test(str);
};

/** 验证固定电话 */
export const isTel = (tel: any): boolean => {
  return /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(tel);
};

/** 验证是否为邮箱 */
export const isEmail = (value: any): boolean => {
  return /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(value);
};

/** 判断对象是否为空 */
export const isNullObj = (params: any): boolean => {
  let flag: boolean = true;
  if (!Object.keys(params).length) {
    return false; // 终止程序
  }
  for (var key in params) {
    if (params[key] != "0" && !params[key]) {
      return false; // 终止程序
    }
  }
  return flag;
};

/** 提取文件后缀名 */
export const funGetSuffix = (str: string): string => {
  if (str.indexOf(".") !== -1) return "";
  const fileExtension = str.substring(str.lastIndexOf(".") + 1);
  return fileExtension;
};

/** 获取某个字符在指定字符串第n次出现的位置 */
export const funFindStrIN = (str: string, cha: string, num: number): number => {
  var x: number = str.indexOf(cha);
  for (var i = 0; i < num; i++) {
    x = str.indexOf(cha, x + 1);
  }
  return x;
};

/** 对象转数组 proxy */
export const funObjChangeList = (obj_data: any): Array<any> => {
  let newDataList: Array<any> = Object.keys(obj_data).map((key): any => {
    return obj_data[key];
  });
  return newDataList;
};

/** 存在小数 保留两位，不存在原路返回 */
export const funNumberPoint = (value: number): string | number => {
  if ((value + "").indexOf(".") !== -1) {
    return value.toFixed(2);
  } else {
    return value;
  }
};

/** 对象排序 以某个键值大小 从小到大 */
export const funObjSortKey = (obj: any, key: string): any => {
  return obj.sort((a: number, b: number): any => {
    let x: number = a[key];
    let y: number = b[key];
    return x < y ? -1 : x > y ? 1 : 0;
  });
};

/** 两个对象合并 只替换新值 */
export const funObjValObj = (a: any, b: any): any => {
  return Object.keys(a)
    .map((v) => ({
      v,
      val: b[v] !== undefined ? b[v] : a[v],
    }))
    .reduce(
      (c, { v, val }) => ({
        ...c,
        [v]: val,
      }),
      {}
    );
};

/** 文件流下载 */
export const funFileDown = (/** blob文件 */ file: any, /** 文件名 */ name: string | number): void => {
  let link: any = document.createElement("a");
  link.href = window.URL.createObjectURL(new Blob([file]));
  link.target = "_blank";
  //文件名和格式
  link.download = name;
  link.style.display = "none";
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

/** 根据超链接下载文件并添加文件名 */
export const funLinkDown = (/** 文件超链接 */ link: string, /** 文件名 */ name: string | number): void => {
  let url: string = link;
  fetch(url).then((res) => {
    res.blob().then((blob) => {
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = name + "";
      a.click();
      window.URL.revokeObjectURL(url);
    });
  });
};

/** 文件防抖下载 */
export const funFilesListUpload = (list: Array<string>): void => {
  for (let i: number = 0; i < list.length; i++) {
    const iframe = document.createElement("iframe");
    iframe.style.display = "none"; // 防止影响页面
    iframe.style.height = "0"; // 防止影响页面
    iframe.src = list[i];
    document.body.appendChild(iframe); // 这一行必须，iframe挂在到dom树上才会发请求
    // 5分钟之后删除
    setTimeout(() => {
      iframe.remove();
    }, 5 * 60 * 1000);
  }
};

/** 中文汉字转数字 */
export const funStrChangeNum = (chnStr: string): number => {
  enum chnNumChar {
    零 = 0,
    一 = 1,
    二 = 2,
    三 = 3,
    四 = 4,
    五 = 5,
    六 = 6,
    七 = 7,
    八 = 8,
    九 = 9,
  }
  var chnNameValue = {
    十: {
      value: 10,
      secUnit: false,
    },
    百: {
      value: 100,
      secUnit: false,
    },
    千: {
      value: 1000,
      secUnit: false,
    },
    万: {
      value: 10000,
      secUnit: true,
    },
    亿: {
      value: 100000000,
      secUnit: true,
    },
  };

  var rtn = 0;
  var section = 0;
  var number = 0;
  var secUnit = false;
  var str = chnStr.split("");
  for (var i = 0; i < str.length; i++) {
    var num = chnNumChar[`${str[i]}`];
    if (typeof num !== "undefined") {
      number = num;
      if (i === str.length - 1) {
        section += number;
      }
    } else {
      var unit = chnNameValue[str[i]].value;
      secUnit = chnNameValue[str[i]].secUnit;
      if (secUnit) {
        section = (section + number) * unit;
        rtn += section;
        section = 0;
      } else {
        section += number * unit;
      }
      number = 0;
    }
  }
  return rtn + section;
};
